function [ Regs] = create_regressors_for_threshold_estim(lz, dflz, Y,X,lx,ly, lcsx, lcsy, indf, lcsz, tau_range, topool, debt)
% this function creates regressors for the threshold_estim function
%
% INPUTS:
%           Y is N x Tmax matrix of observations on dependent variable
%           X is N x Tmax x kx matrix of observations on k regressors from which cs averages are computed as well
%           lx is the number of lags for X regressors
%           ly is the number of lags for the dependent variable
%           lcsx is the number of lags on cross section averages of variables in X
%           lcsy is the number of lags on cross section averages of the dependent variable (Y)
%
%     indf          pointer to an indicator function z=indf(Zindf,tau)
%     tau_range     ktau X 1 vector of the set of values for the threshold tau
%     lcsz      the number of lags on cross section averages of the indizator variable
%
%
% OUTPUTS:
%      Regs structure with the following fields
%          .N number of countries
%          .y  1xN cell with elements being Ti x 1 vector of the observations on the dependent variable in country i
%          .T  N x 1  vector with elementes Ti (the number of time observations for country i)
%          .x 1xN cell with elements being  Ti x h matrices of regressors 
%           (including constant, lagged dep vars, contemporaneous and lagged vars, cs averages of y and lags, cs averages of x and lags)
%          .posxi 1xN cell with elements 1x2 vectors [start,end] defining position of the levels of X in x{i}. If X is absent, then the position is [0,0] 
%          .posylags 1xN cell with elements 1x2 vectors [start,end] defining position of the lags of Y in x{i}. If ylags are absent, then the position is [0,0] 
%          .topool h x 1 vector of boolean variables (1 pool, 0 not)
%          .tau_range    passes on tau_range input
%          .z


CS_demeaned_ave=1;

[N,Tmax]=size(Y);
T=zeros(N,1);
for i=1:N          
            [yi,xi, zi, zsa, zlagsa, pos_xi, pos_ylags, pos_stars]=create_regressors_ci(Y,X, lx, ly, lcsx, lcsy,indf, lcsz,tau_range, CS_demeaned_ave,i, debt, lz, dflz);
            Ti=size(yi,1);
            T(i,1)=Ti;
            y{i}=yi;
            x{i}=xi;
            z{i}=zi;
            zs{i}=zsa;
            zlags{i}=zlagsa;
            posxi{i}=pos_xi;
            posylags{i}=pos_ylags;
            posstars{i}=pos_stars;
end

[pom,h]=size(xi);

Regs.N=N;
Regs.y=y;
Regs.T=T;
Regs.x=x;
if not(size(topool,1)==h)
    'Dimension of topool does not reflect the choice of variables!'
    dimension_of_topool_should_be=h
    dbstop
end
Regs.topool=topool;
Regs.tau_range=tau_range;
Regs.zs=zs;
Regs.z=z;
Regs.zlags=zlags;
Regs.posxi=posxi;
Regs.posylags=posylags;
Regs.posstars=posstars;

